package com.ant.http

import okhttp3.Interceptor

/**
 * copyright：
 * @author：anthui creation date： .
 * @since: 1.0.0
 * @describe:
 */

object HttpManager {


    class Builder {


        fun setBaseUrl(url: String): Builder {
            HttpConfig.HTTP_BASE_URL = url
            return this
        }

        /**
         * copyright：
         * @author：anthui creation date： 2020/11/18.
         * @since: 1.0.0
         * @describe: 添加拦截器
         */
        fun addInterceptor(interceptor: Interceptor): Builder {
            HttpConfig.mInterceptors.add(interceptor)
            return this
        }


        /**
         * copyright：
         * @author：anthui creation date： 2020/11/18.
         * @since: 1.0.0
         * @describe: 批量添加拦截器
         */
        fun addInterceptorList(listInterceptor: List<Interceptor>): Builder {
            HttpConfig.mInterceptors.addAll(listInterceptor)
            return this
        }

        /**
         * copyright：
         * @author：anthui creation date： 2020/11/18.
         * @since: 1.0.0
         * @describe: 是否处于debug调试环境
         */
        fun isDebug(isDebug: Boolean): Builder {
            HttpConfig.isDebug = isDebug

            return this
        }

        /**
         * copyright：
         * @author：anthui creation date： 2020/11/18.
         * @since: 1.0.0
         * @describe: 是否允许走代理（抓包）
         */
        fun proxyEnable(isDebug: Boolean): Builder {
            HttpConfig.proxyEnable = isDebug
            return this
        }

        /**
         * copyright：
         * @author：anthui creation date： 2020/11/18.
         * @since: 1.0.0
         * @describe: 设置超时时间
         */

        fun connectTimeout(time: Long): Builder {
            HttpConfig.HTTP_TIME_OUT_CONNECTION = time
            HttpConfig.HTTP_TIME_OUT_WRITE = time
            HttpConfig.HTTP_TIME_OUT_READE = time
            return this

        }

        fun build() {
            "HttpManger 初始化".println()

            if (HttpConfig.isInitHttpConfig) {
                "okHttpClient 已经初始化过，请勿重复初始化，OkHttpClient只会初始化一遍，后续初始化不会生效".println()
            }
            HttpConfig.isInitHttpConfig = true
        }

    }


    //一个基础域名对应一个HttClient,一个HttpClient对应 一个Retrofit
    private val httpMap =
        HashMap<String, HttpClient>()

    //默认使用 HttpConfig.HTTP_BASE_URL 地址
    fun getRetrofit(baseUrl: String): HttpClient {

        if (baseUrl.isEmpty()) {
            "baseUrl 不允许为空".println()
        }
        //缓存中已经有 直接获取，没有，就创建
        if (httpMap.containsKey(baseUrl)) {
            httpMap[baseUrl]?.let {
                return it
            }
        }
        val httpClient = HttpClient(baseUrl)
        httpMap[baseUrl] = httpClient
        //创建新的 并返回
        return httpClient
    }

    /**
     * copyright：
     * @author：anthui creation date： 2020/11/18.
     * @since: 1.0.0
     * @describe: 获取默认Retrofit对象
     */
    fun <T : HttpService> getService(clazz: Class<T>): T {
        return getRetrofit(HttpConfig.HTTP_BASE_URL).getService(clazz)
    }


}

fun String.println() {

    println("HttpManager --> $this")

}